package student

import (
    "cqsx/database"
    "crypto/md5"
	"fmt"
    "os"
	"time"
	"github.com/howeyc/gopass"
	"gorm.io/gorm"
)



var db *gorm.DB

// 49ba59abbe56e057  123456/
// f0bf350d99ab47cb  356456/
// 登录

func Login() bool {
	var name string
	fmt.Println("姓名")
	fmt.Scanln(&name)
	var pswd string
	fmt.Println("密码")
	pswdBytes, err := gopass.GetPasswdPrompt("密码", true, os.Stdin, os.Stdout)
	if err != nil {
		fmt.Println("GetPasswdPrompt 失败,", err.Error())
		return false
	}
	pswd = string(pswdBytes)
	pswd = fmt.Sprintf("%x", md5.Sum([]byte(pswd)))
	var user Classmate
	db.Raw("SELECT *FROM classmates. classmates where name= ?", name).Scan(&user)
	if user.Pswd == pswd {
		fmt.Println("成功", user.Name)
		return true
	} else {
		fmt.Println("失败")
		return false
	}
}
type Record struct {
	ID         int
	Name       string
	Sn         string
	Createtime time.Time
	Classroom  string
	Comments   string
}

func ResetPswd() {
	//验证密码
	var oldPswd string
	fmt.Println("请输入你现在的密码进行身份验证")
	fmt.Scanln(&oldPswd)
	oldPswd = fmt.Sprintf("%x", md5.Sum([]byte(oldPswd)))
	if loginUser.Pswd != oldPswd {
		fmt.Println("密码错误")
		return
	}

	//输入新密码
	var newPswd string
	fmt.Println("输入新密码")
	fmt.Scanln("&newPswd")

	//再输入新密码
	var newPswdAgain string
	fmt.Println("请再输入你的新密码")
	fmt.Scanln(&newPswdAgain)
	if newPswd != newPswdAgain {
		fmt.Println("你两次输入的密码不一致")
		return
	}
	//将新密码存入数据
	loginUser.Pswd = fmt.Sprintf("%x", md5.Sum([]byte(newPswd)))
	db.Save(&loginUser)
	fmt.Println("修改密码成功")
}

// 对应数据库里的 records 表
type Records struct {
	Id         int
	Name       string
	Sn         string
	Tel        string
	Comments   string
	Classid    int
	Createtime time.Time
	Classroom  string
}

// 加分函数
func JiaFen() {
	//输入姓名
	var name string
	fmt.Println("输入修改考勤的姓名")
	fmt.Println(&name)
	//查出所有考勤记录
	var recordsList []Records
	database.DB.Raw("SELECT *from classmates. records where name= ?", name).Scan(&recordsList)
	for _, record := range recordsList {
		fmt.Printf("id:%5d | 姓名:%10s | 分数:%10v | 时间:%20v \n", record.Id, record.Name, record.Comments, record.Createtime)
	}
	//根据查询信息，修改考勤信息
	fmt.Println("输入考勤id")
	var recordid int
	fmt.Scanln(&recordid)
	fmt.Println("输入添加分数")
	var fenshu string
	fmt.Println(&fenshu)
	database.DB.Exec("update classmates.records set comments= ? where id = ?", fenshu, recordid)
	fmt.Println("加分成功")
	//成功后，显示考勤记录
	database.DB.Raw("SELECT *from classmates. records where name= ?", name).Scan(&recordsList)
	for _, record := range recordsList {
		fmt.Printf("id:%5d | 姓名:%10s | 分数:%10v | 时间:%20v \n", record.Id, record.Name, record.Comments, record.Createtime)
	}
}

type Logs struct {
	ID          int
	OpName      string
	OpId        int
	Createtime  time.Time
	Content     string
	BeOperateSN string
}

// 自动写日志
func AutoWriteLog(operName string, operID int, shijian time.Time, content string, beOpSN string) {
	var thisLog Logs
	thisLog.BeOperateSN = beOpSN
	thisLog.Content = content
	thisLog.OpName = operName
	thisLog.OpId = operID
	thisLog.Createtime = shijian
	database.DB.Save(&thisLog)
}

// 转账
func zhuanzhang() {
	//登录的人给第三方转账
	var targetName string
	fmt.Println("输入接受姓名")
	fmt.Scanln(&targetName)
	//查询资料
	var targetList []*database.Classmate
	database.DB.Raw("SELECT *from classmates. records where name= ?", targetName).Scan(&targetList)
	fmt.Println("同名时，查询学号")
	for _, person := range targetList {
		fmt.Printf("sn", person.Sn, "name", person.Name, "ID", person.Id)
	}
	//收账人id和收账金额
	var targetID int
	var edu float64
	fmt.Println("输入id")
	fmt.Scanln(&targetID)
	fmt.Println("输入金额")
	fmt.Scanln(&edu)
	//登录人的钱减少
	loginUser.Money -= edu
	//双方信息存入数据库
	database.DB.Begin()
	database.DB.Save(loginUser)
	database.DB.Exec("UPDATE classmates.classmates ste money=(money+?) where id=?", edu, targetID)
	fmt.Println("转账成功")
}
// 保存考勤记录
// 查出每个学生的考勤记录，写入相对应的文本中。
func SaveStudentRecords() {
	//创建程序
	var allStudents []database.Classmate
	database.DB.Raw("select * from classmates.classmates").Scan(&allStudents)
	for _, stu := range allStudents {
		currentFile, err := os.Create("D:/Go/class2/" + stu.Sn + stu.Name + ".txt")
		if err != nil {
			fmt.Println("创建文件夹失败,原因是:", err)
			return
		}
		var thisStudentRecords []Records
		database.DB.Raw(" SELECT * FROM classmates.records WHERE sn =?", stu.Sn).Scan(&thisStudentRecords)
		currentFile.WriteString("面向对象程序设计的考勤记录＼ n ")
		for _, record := range thisStudentRecords {
			currentFile.WriteString(record.Name + record.Createtime.String() + record.Comments + "\n")
		}
		currentFile.WriteString("以上就是该同学的所有考勤记录。登记时间是：" + time.Now().String())
		currentFile.Close()
	}
}

func ReadFile() {
	// D:/Go/class2/202220222995许远.txt
	thisFile, err := os.OpenFile(" D:/Go/class2/202220222995许远.txt", os.O_RDWR, 0755)
	if err != nil {
		fmt.Println("在打开文件的时候，出错，错误是：", err)
		return
	}
	var neirong []byte = make([]byte, 645)
	thisFile.Read(neirong)
	fmt.Println("读出来的内容是：", string(neirong))
	thisFile.WriteString("\n 我已经检查过你的作业了")
	thisFile.WriteString("是否似懂非懂")
	thisFile.Close()
	err = os.Remove(" D:/Go/class2/202220222993赵丽娜.txt ")
	if err != nil {
		fmt.Println("删除文件的时候出错：", err)
		return
	}
	fmt.Println("已经被删掉了")
	err = os.Rename("D:/Go/class2/202220222995许远txt", "D:/Go/class2/202220222995许远.txt")
	if err != nil {
		fmt.Println("改名的时候出错，", err)
		return
	}
	fmt.Println("已经被改名了")
	fmt.Println(os.Args)

	for _, cmd := range os.Args {
		switch cmd {
		case " run ":
			fmt.Println("你是想运行某某东西")
		case " test ":
			fmt.Println("你是想测试某某东西")
		case " xiaban ":
			fmt.Println("你是想下班了")
		}
		os.Mkdir(" D:/ fdffddf ", 0755)
		os.MkdirAll(" D:/Go/fff/asdfsad/sadf/asdf/asdf/asdfsa/df", 0755)
		time.Sleep(time.Minute * 10)
	}
}
func OpenDir() {
	var dir string
	fmt.Println("请输入你要打开的目录")
	fmt.Scanln(&dir)
	dirfile, err := os.Open(dir)
	if err != nil {
		fmt.Println("打开失败，", err)
		return
	}
	studentFiles, err := dirfile.ReadDir(50)
	if err != nil {
		fmt.Println("读取失败，", err)
		return
	}
	for _, file := range studentFiles {
		fileinfo, _ := file.Info()
		if time.Now().Add(-time.Hour * 24 * 3).After(fileinfo.ModTime()) {
			fmt.Println("这是一个比较老的文件")
		} else {
			fmt.Println("这是一个3天内被修改过的文件")
			fmt.Println(file.Name(), "文件大小：", fileinfo.Size(), "修改时间：", fileinfo.ModTime(), fileinfo.Mode())
		}
	}
}
